package smedi.ismedi.mobile.bff.controller.qualityteam;


import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.qualityteam.dto.request.team.QueryTeamPageRequest;
import com.smedi.ismedi.qualityteam.dto.request.team.TeamExportRequest;
import com.smedi.ismedi.qualityteam.dto.request.team.TeamRequest;
import com.smedi.ismedi.qualityteam.dto.response.AttachmentResponse;
import com.smedi.ismedi.qualityteam.dto.response.team.TeamDetailResponse;
import com.smedi.ismedi.qualityteam.dto.response.team.TeamResponse;
import com.smedi.ismedi.qualityteam.feign.RemoteTeamFeignApi;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.mobile.bff.controller.BaseController;
import smedi.ismedi.mobile.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author nyl
 * @Date 2021/6/17 9:40
 */
@Api(tags = {"活动课题接口"})
@Slf4j
@RequestMapping(value = "/mobilebff/v1/team")
@RestController
@RequiredArgsConstructor
public class TeamController extends BaseController {
    private final RemoteTeamFeignApi remoteTeamFeignApi;

    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;

    @ApiOperation(value = "活动小组 新增", nickname = "活动小组 新增")
    @PostMapping(value = "/add")
    public ResponseData<TeamResponse> addTeam(@Validated @RequestBody TeamRequest teamRequest) {

        return remoteTeamFeignApi.addTeam(teamRequest);
    }

    /**
     * 活动小组详情查询
     *
     * @param teamId
     * @return
     */
    @ApiOperation(value = "活动小组详情查询", nickname = "活动小组详情查询")
    @GetMapping(value = "/searchTeamDetail")
    ResponseData<TeamDetailResponse> searchTeamDetail(@RequestParam("teamId") Long teamId) {

        return remoteTeamFeignApi.searchTeamDetail(teamId);
    }

    /**
     * 获取 活动小组 分页
     *
     * @param queryTeamPageRequest 活动小组查询
     * @return ResponseData<TeamResponse>
     */
    @ApiOperation(httpMethod = "POST", value = "获取 活动小组 分页")
    @PostMapping(value = "/page")
    ResponseData<PaginationData<TeamResponse>> getTeamPage(@Validated @RequestBody QueryTeamPageRequest queryTeamPageRequest) {
        return remoteTeamFeignApi.getTeamPage(queryTeamPageRequest);
    }

    /**
     * @param teamExportRequest
     */
    @ApiOperation(httpMethod = "POST", value = "导出数据")
    @PostMapping(value = "/export")
    ResponseData<List<AttachmentResponse>> exportTeam(@Validated @RequestBody TeamExportRequest teamExportRequest, HttpServletResponse response) {
        List<AttachmentResponse> signKeys = remoteTeamFeignApi.exportTeam(teamExportRequest).getData();
//        if (CollectionUtils.isEmpty(signKeys)) {
//            return new ResponseData<>().success(new ArrayList<>());
//        }
//        List<FileInfoResponse> fileInfoResponse = fileFeignApi.getFileList(signKeys).getData();
//        for (FileInfoResponse file : fileInfoResponse) {
//            FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, file.getStorageGroup(), file.getPath(), "质量管理小组（课题）注册登记表", response);
//        }
//        if (CollectionUtils.isNotEmpty(fileInfoResponse)) {
//            fileInfoResponse.forEach(x -> x.setName("质量管理小组（课题）注册登记表"));
//        }
        return new ResponseData<>().success(signKeys);
    }

    /**
     * 根据下载密钥下载文件
     *
     * @param signKey
     * @return
     */
    @ApiOperation(value = "根据下载密钥下载文件", nickname = "根据下载密钥下载文件", response = ResponseData.class)
    @GetMapping(value = "/downloadTeamFileBySignKey")
    ResponseData<Boolean> downloadFileBySignKey(@NotNull @ApiParam(value = "signKey", required = true)
                                                @RequestParam(value = "signKey", required = true) String signKey, HttpServletResponse response) {
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        if (fileInfoResponse == null) {
            return new ResponseData<Boolean>().failure("file", "9999", "下载失败");
        }
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), "质量管理小组（课题）注册登记表", response);
        return new ResponseData<>().success(true);
    }

}
